#ifndef CANVAS_H
#define CANVAS_H

#include "commons.h"
#include "imageitem.h"

class Canvas : public ImageItem
{
    Q_OBJECT
public:

    Canvas(const QString &imgName,
           const QPoint &leftTop,
           const QPoint &rightBottom,
           QGraphicsScene *scene = 0,
           QGraphicsItem *parent = 0);

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

    inline const QLinkedList<Point*>& getPointsList() {return pointsList;}
    inline const QLinkedList<Line*>& getLinesList() {return linesList;}


    void setState(const CanvasState state);
    inline CanvasState getState() const {return state;}
    bool isNearPoints(const QPointF& p1, const QPointF& p2) const;
    void setNameBanner(const QString &name);
    void clear();
    void copyGraph(const QLinkedList<Point *>& pl, const QLinkedList<Line *>& ll);

signals:
    void pointHoverEnter();
    void pointHoverLeave();
    void lineHoverEnter();
    void lineHoverLeave();
    void linerHoverEnter();
    void linerHoverLeave();

    void canvasHoverEnter(CanvasState state);
    void canvasHoverLeave();

    void pointDragStart();
    void pointDragStop();
    void lineDragStart();
    void lineDragStop();

    void lineringStop();

public slots:
    void aPointHasHoverEnter();
    void aPointHasHoverLeave();
    void aPointHasDragStart();
    void aPointHasDragStop();
    void aPointHasLinerStart(Point* point);
    void aPointHasMove(Point* point);

    void aLineHasHoverEnter();
    void aLineHasHoverLeave();
    void aLineHasDragStart(Line* line);
    void aLineHasDragStop(Line* line);
    void aLineHasMove(Line* line);
    void aPointNeedDelete(Point* point);
    void aLineNeedDelete(Line* line);

private:
    CanvasState state;
    CanvasState prevState;
    QGraphicsTextItem *posBanner;
    QGraphicsTextItem *nameBanner;

    QLinkedList<Point *> pointsList;
    QLinkedList<Line *> linesList;

    Line *current;

    void initEventSystem(Point* p);
    void initEventSystem(Line* l);
    Point *findNearbyPoint(const QPointF& pos);
    void detatchPoint(Point* point);
};

#endif // CANVAS_H
